package com.hbsx.why.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;

import butterknife.BindView;

/**
 * Created by liub on 2017/7/6 09:21.
 */

public class WebViewActivity extends BaseActivity {
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.my_webview)
    WebView myWebview;
    String title, url;
    private Context context = this;
    private ProgressDialog progressDialog;

    @Override
    protected void initView() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");

        progressDialog = new ProgressDialog(context);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
        if (title == null || title == "") {
            title = "webview  demo";
        }
        tvTopTitle.setText(title);
        if (url == null || url == "") {
            url = "http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%C3%C0%C5%AE&fr=ala&ala=1&alatpl=cover&pos=0&hs=2&xthttps=000000";
        }
        myWebview.loadUrl(url);
        myWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口，如window.open()，默认为false
        myWebview.getSettings().setJavaScriptEnabled(true);//是否允许执行js，默认为false。设置true时，会提醒可能造成XSS漏洞
        myWebview.getSettings().setSupportZoom(true);//是否可以缩放，默认true
        myWebview.getSettings().setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        myWebview.getSettings().setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        myWebview.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        myWebview.getSettings().setAppCacheEnabled(true);//是否使用缓存
        myWebview.getSettings().setDomStorageEnabled(true);//DOM Storage
        myWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去myWebview打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                progressDialog.dismiss();
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_webview;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myWebview.clearHistory();
        myWebview.clearCache(true);
        myWebview.clearView();
        myWebview.destroy();
        myWebview = null;
    }
}
